package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.QuestionMapper;
import com.tanhua.model.domain.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询陌生人问题
     * @param userId
     * @return
     */
    @Override
    public Question findByUserId(Long userId) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId != null, Question::getUserId, userId);
        return questionMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public void save(Question question) {
        questionMapper.insert(question);
    }

    @Override
    public void update(Question question) {
        questionMapper.updateById(question);
    }

    @Override
    public void myPostQuestion(Question question) {
        Long userId = question.getUserId();
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getUserId,userId);
        questionMapper.update(question,lambdaQueryWrapper);
    }

    @Override
    public void myPutQuestion(Question question) {
        Long userId = question.getUserId();
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getUserId,userId);
        questionMapper.update(question,lambdaQueryWrapper);
    }
}